//
//  UINavigationController-Extension.swift
//  NAVPushDemo
//
//  Created by point on 2018/5/11.
//  Copyright © 2018年 whiteboard. All rights reserved.
//

import UIKit

extension UINavigationController {
    
    /// 存在即pop 不存在即push
    ///
    /// - Parameters:
    ///   - currentClass: 当前对象   从哪里来
    ///   - vcClassName:  字符串     到哪里去
    ///   - dict: 数据               去干啥
    ///   - sort: 0:最上层  1:最底层  默认是最上层的
    
    
    func dcPushORPop<T>(_ currentClass:AnyClass , _ vcClassName: String , _ casual:T  , _ sort:Int = 0){
        
        let classNameArr = NSStringFromClass(currentClass).split(separator: ".")
        
        guard let sourceVC = classNameArr.last as NSString? as String?  else { return }
        
        guard   let NameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"]as? String else{
            return
        }
        guard  let TrueVC = NSClassFromString(NameSpace + "." + vcClassName)else{
            return
        }
        guard let Type = TrueVC as? UIViewController.Type else{
            return
        }
        //排序顺序
        let tempviewControllers = sort == 0 ? viewControllers.reversed() : viewControllers
        //栈内是否存在
        let contain = tempviewControllers.contains(where: { (vc:UIViewController) -> Bool in
            return vc.classForCoder == TrueVC
        })
        if contain { //存在即pop
            for (index, vc) in viewControllers.enumerated() {
                if vc.classForCoder == TrueVC {
                    let popVC = viewControllers[index]
                    vc.dcSourceVc = sourceVC
                    vc.dcObjcet?.value = casual
                    self.popToViewController(popVC, animated: true)
                    return
                }
            }
        }else { //不存在即push
            let vc = Type.init()
            vc.dcSourceVc = sourceVC
            vc.dcObjcet?.value = casual
            self.pushViewController(vc, animated: true)
        }
    }
}



extension UINavigationController: UINavigationBarDelegate, UIGestureRecognizerDelegate {
    //左侧按钮的代理事件
    public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
        if viewControllers.count < (navigationBar.items?.count)! {
            return true
        }
        var shouldPop = false
        let vc: UIViewController = topViewController!
        if vc.responds(to: #selector(navigationShouldPopMethod)) {
            shouldPop = vc.navigationShouldPopMethod()
        }
        if shouldPop {
            DispatchQueue.main.async {
                self.popViewController(animated: true)
            }
        } else {
            for subview in navigationBar.subviews {
                if 0.0 < subview.alpha && subview.alpha < 1.0 {
                    UIView.animate(withDuration: 0.25) {
                        subview.alpha = 1.0
                    }
                }
            }
        }
        return false
    }
    
    //手势用于侧滑
    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if children.count == 1 {
            return false
        } else {
            if topViewController?.responds(to: #selector(navigationShouldPopMethod)) != nil {
                return topViewController!.navigationShouldPopMethod()
            }
            return true
        }
    }
}



